
var monthNames=new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var monthOffset = 0;
var yearOffset;
var Parses;

function FillCalendarCell( index, d, m, currentMonth ){
	
	var o = $("#ParseCalendar td:eq("+(index+7)+")");
	o.html('');
	o.removeClass('CurMonth')
	o.append('<i class="Day">'+ord(d)+'</i>');
	if( currentMonth ) o.addClass('CurMonth');
	
	// get parses started on this day
	if( typeof Parses != 'undefined' ){
		var dayStart = Math.floor(new Date(2012,m,d).getTime()/1000);
		for( var i = 0; i < Parses.length; i++ ){
			if( Parses[i][2] >= dayStart && Parses[i][2] <= dayStart+86400 ){
				var offset = Math.floor((Parses[i][2]-dayStart)/(60*15));
				var width = Math.floor(Parses[i][3]/(60*15));
				var duration_h = Math.floor((Parses[i][3]/3600));
				var duration_m = Math.floor(((Parses[i][3]%3600)/60));
				var ranks = '';
				var rankTier = '';
				for( var j = 0; j < Parses[i][6].length; j++ ){
					if( Parses[i][6][j][0] <= 3 ) { rankTier = 'Legendary'; }
					else if( Parses[i][6][j][0] <= 10 ) { rankTier = 'Epic'; }
					else if( Parses[i][6][j][0] <= 25 ) { rankTier = 'Superior'; }
					else rankTier = 'Uncommon';
					
					ranks += '<span class="Rank"><b class="'+rankTier+'">'+Parses[i][6][j][0]+'</b> '+Parses[i][6][j][4]+' (<span class="Class'+Parses[i][6][j][2]+'">'+Parses[i][6][j][1]+'</span>)</span><br/>';
				}
				if( duration_h > 0 ) { var duration = duration_h+' Hours '+duration_m+' Minutes'; }
				else { var duration = duration_m+' Minutes'; }
				o.prepend(
					'<div class="Parse" style="margin-left:'+offset+'px;width:'+width+'px;">'+
						'<a class="ParseLink" href="http://worldoflogs.com/reports/'+Parses[i][4]+'"></a>'+
						'<div class="Tooltip">'+
							'<a href="http://worldoflogs.com/reports/'+Parses[i][4]+'">'+Parses[i][0]+'</a><br/>'+
							'Duration: <i>'+duration+'</i><br/>'+
							'Uploaded by <a href="#">'+Parses[i][5]+'</a><br/>'+
							'<i class="Comment">'+Parses[i][1]+'</i><br/>'+
							''+ranks+
						'</div>'+
					'</div>'
				);
			}
		}
	}
}

function FillCalendar() {
	var now = new Date();
	var m = (now.getMonth()+monthOffset) % 12;
	if( m < 0 ) m = 12+m;
	
	$("#CalendarTitle").html(monthNames[m]);
	
	var y = now.getFullYear();
	var starti = new Date( y, m, 1 ).getDay()-1;
	var endi = starti+DaysInMonth( m, y );
	var lastMonthDays = DaysInMonth( m-1, y );
	for(var i=0;i<=starti;i++) { FillCalendarCell( i, lastMonthDays-(starti-i), m-1, false ); }//last month days
	for(var i=starti+1;i<=endi;i++) { FillCalendarCell( i, i-starti, m, true ); }//current month days
	for(var i=endi+1;i<35;i++) { FillCalendarCell( i, i-endi, m+1, false ); }//next months days
}

function DaysInMonth(m,y){
	if ( m==3 || m==5 || m==8 || m==10 ) return 30;
	if ( m==1 ) return ((y%4==0) && (y%100!=0) || (y%400==0)) ? 29 : 28;
	return 31;
}

function ord(n) { // by rh @ highdots.com
	var sfx = ["th","st","nd","rd"];
	var val = n%100;
	return n + (sfx[(val-20)%10] || sfx[val] || sfx[0]);
}
function offsetMonth( val ){
	monthOffset += val;
}

$(document).ready(function(){
	FillCalendar();
	$("#NextMonth").click( function(e){ e.preventDefault(); offsetMonth(1); FillCalendar(); } );
	$("#PrevMonth").click( function(e){ e.preventDefault(); offsetMonth(-1); FillCalendar(); } );
});